<template>
    <div class="layout-pd">
      <zh-space style="padding-top: 10px">
        <zh-card title="">
          <zh-form ref="queryForm" :model="state.queryParam">
            <zh-form-select
                prop="objetcType"
                label="授权类型"
                placeholder="单选"
                :options="state.dictData.objectType"
                v-model="state.queryParam.objetcType"
                :allowClear="false"
            ></zh-form-select>
            <zh-form-select
                prop="userType"
                label="用户类型"
                placeholder="单选"
                :options="state.dictData.userType"
                v-model="state.queryParam.userType"
            ></zh-form-select>
            <zh-form-query v-model="state.advanced" :form-ref="queryForm" :table-ref="tableRef">
              <zh-button v-func:sysDefaultPrivilege0005 style="margin-left: 8px" button-type="add" @click="add"> 新增 </zh-button>
            </zh-form-query>
          </zh-form>
        </zh-card>
        <zh-table
            ref="tableRef"
            title=""
            :height="600"
            funcCode="sysDefaultPrivilege0001"
            :columns="state.tableColumn"
            :disabled="false"
            highlight-current-row
        >
          <template #operate_default="{ row }">
            <zh-button v-func:sysDefaultPrivilege0005  link  button-type="edit" @click="edit(row)">编辑 </zh-button>
            <zh-button v-func:role0006 link button-type="del" @click="del(row)">删除 </zh-button>
          </template>
        </zh-table>
      </zh-space>
  
      <zh-dialog :title="state.modalTitle" width="50%" v-model="state.visible" @confirm="save">
        <defaultPriForm ref="defaultPriPage" :info="state.selectRow" v-model="state.visible" />
      </zh-dialog>
     
    </div>
  </template>
  
  <script lang="ts" setup name="defaultPriManager">
  import defaultPriForm from './component/defaultPriForm.vue';
  import { ElMessageBox,ElMessage } from 'element-plus';
  
  //------------------定义子组件---------------------------
  const queryForm = ref<any>({});
  const tableRef = ref<any>({});
  const defaultPriPage = ref<any>({});
  
  //------------------内部变量定义-------------------------
  const state = reactive({
    description: '默认权限管理',
    dictData: {} as any,
    visible: false,
    form: null,
    // 高级搜索 展开/关闭
    advanced: false,
    // 查询参数
    queryParam: {objetcType:'role'} as any,
    // 表头
    loading: false,
    tablePage: {
      total: 0,
      currentPage: 1,
      pageNum: 1,
      pageSize: 20,
    },
    tableColumn: [
      { type: 'seq', width: 80, fixed: 'left', align: 'center', title: '序号' },
      { field: 'id', title: 'ID', fixed: 'left', visible: false, align: 'center', width: 150 },
      { field: 'userTypeName', title: '用户类型', align: 'center', fixed: 'left', width: 200 },
      { field: 'userType', title: '用户类型编码', visible: false, align: 'center', width: 200 },
      { field: 'objetcType', title: '权限类型编码', visible: false, align: 'center', width: 100 },
      { field: 'objetcTypeName', title: '权限类型', align: 'center', width: 200 },
      { field: 'objectId', title: '权限id', visible: false, align: 'center', width: 200 },
      { field: 'objectName', title: '权限名称', visible: true, align: 'center', width: 200 },
      { field: 'operate', title: '操作', align: 'center', fixed: 'right', width: 200, slots: { default: 'operate_default' } },
    ],
    selectRow: null as any,
    modalTitle: '操作',
  });
  
  //------------------定义调用方法-------------------------
  function findList() {
    tableRef.value.query(state.queryParam);
  }
  function refreshList() {
    tableRef.value.refresh(state.queryParam);
  }
 
  function add() {
    state.modalTitle = '新增信息';
    state.visible = true;
    state.selectRow={objetcType:'role'}
  }
  function edit(row) {
    state.selectRow = row;
    state.modalTitle = '修改信息';
    state.visible = true;
  }
  function save() {
    defaultPriPage.value.save().then((row) => {
      findList();
    });
  }
  function del(row) {
    ElMessageBox.confirm('是否确认删除？').then(() => {
      const params = {
        id: row.id,
      };
      setTimeout(() => {
        $api.common.send
          .post('sysDefaultPrivilege0006', params)
          .then(() => {
            $messageBox.success('删除成功', '提示');
            refreshList();
          })
        .catch(() => {});
      }, 100);
    });
  
    
  }


  
  //------------------初始化------------------------------
  
  // 数据字典初始化
  const initDict = () => {
    const params = {
      dictFields: 'userType,objectType',
    };
    $api.common.getBatchDict.post(params).then((res) => {
      state.dictData = res.result;
    });
  };
  
  onMounted(() => {
    initDict();
    nextTick(() => {
      findList();
    });
  });
  </script>
  <style lang="scss" scoped></style>